package domain

import (
	"net/http"
	"log"
	"io/ioutil"
	"strings"
	"hezj.com/dd/module/common/param"
	"math/rand"
	"time"
	"hezj.com/dd/module/common/utils"
	"crypto/tls"
)

type DDma struct {
	User param.UserParam
	ddCodeSize int
	randomSleepSecond int64
}


func (this *DDma) ClickLoop()  {

	for{
		currentTime := time.Now()
		currentHour := currentTime.Hour()
		if currentHour >= this.User.PauseHouseStart || currentHour <= this.User.PauseHouseEnd {
			log.Printf("休息时间：%v-%v", this.User.PauseHouseStart, this.User.PauseHouseEnd)
			time.Sleep(time.Hour)
		}
		this.ClickOne()
		this.Sleep()
	}
}


func (this *DDma) ClickOne()  {
	this.Click(this.GetDdCode())
}

func (this *DDma)GetRandomSleepSecond() int64 {
	if this.randomSleepSecond<1{
		this.randomSleepSecond= this.User.MaxSleepSecond-this.User.MinSleepSecond
	}
	return this.randomSleepSecond
}

func (this *DDma) Sleep()  {
	sleepSecond:=rand.Int63n(this.GetRandomSleepSecond())
	log.Printf("暂停 %v 秒",sleepSecond)
	time.Sleep(time.Duration(sleepSecond)*time.Second)
}


func (this *DDma) GetDdCode() string {
	randomIndex:=rand.Intn(this.GetDdCodeLength())
	ddCode:=this.User.DdCode[randomIndex]
	return ddCode
}

func (this *DDma)GetDdCodeLength()int  {
	if this.ddCodeSize<1{
		this.ddCodeSize= len(this.User.DdCode)
	}
	return this.ddCodeSize
}

func (this * DDma)Click(shortCode string) (string,bool,error) {
	return this.ClickByIp(shortCode,utils.GetRandomIp())
}

func (*DDma) ClickByIp(shortCode, ip string) (string, bool, error) {
	tr := &http.Transport{
		TLSClientConfig:    &tls.Config{InsecureSkipVerify: true},
		DisableCompression: true,
	}
	client := &http.Client{Transport:tr}
	url := "http://dd.ma/counter.php?c="+shortCode+"&rf="
	//url := "http://www.baidu.com"

	req, err := http.NewRequest("GET", url, strings.NewReader(""))
	if err != nil {
		log.Printf(err.Error())
		return "", false, err
	}

	headerMap := getHeaderMap(shortCode, ip)
	for key, value := range headerMap {
		req.Header.Add(key, value)
	}

	resp, err := client.Do(req)
	if err != nil {
		log.Printf(err.Error())
		return "", false, err
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Printf(err.Error())
		return "", false, err
	}
	bodyStr := string(body)
	log.Printf("请求结果：%s", bodyStr)
	return bodyStr, false, nil
}


func getParamMap()map[string]string{
	paramMap:=map[string]string{}
	return paramMap
}
func getHeaderMap(shortCode,ip string)map[string]string{
	headerMap:=map[string]string{}
	headerMap["Pragma"]="no-cache"
	headerMap["Referer"]="http://dd.ma/"+shortCode
	headerMap["User-Agent"]=param.GetUserAgentParam().GetRandomUserAgent()
	//headerMap["User-Agent"]="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0"
	//headerMap["x-csrf-token"]="yrdoaHXGiyN7"
	//headerMap["x-requested-with"]="XMLHttpRequest"
	headerMap["Accept"]="*/*"
	headerMap["Host"]="dd.ma"
	headerMap["Accept-encoding"]="deflate"
	headerMap["Accept-language"]="zh-CN,zh;q=0.8"
	headerMap["Cache-control"]="no-cache"
	headerMap["X-Forwarded-For"]=ip
	//headerMap["Proxy-Client-IP"]=utils.GetRandomIp()
	//headerMap["WL-Proxy-Client-IP"]=utils.GetRandomIp()
	//headerMap["HTTP_CLIENT_IP"]=utils.GetRandomIp()
	//headerMap["X-Real-IP"]=utils.GetRandomIp()
	return headerMap
}